<?php 
	$table = $this->table;
	$search_mode = $this->search_mode;
?>

<!-- Hiển thị trong trường hợp thông thường -->
<?php if(!$search_mode):?>

<section class="grid_12"> <!-- pagination table -->
	<!-- 検索フォーム -->
	<div class="block-border" style="margin-bottom: 20px;">
    	<div class="block-content">
	        <h1>検索</h1>
	       <form method="post" class="form" action="/admin/user" id="search_form">
	        	<p>
        			<label for="search_type" class="inline">検索タイプ</label>
		        	<select id="search_type" name="search_type">
		        		<option value="all">全部</option>
		        		<option value="username">ユーザ名</option>
		        		<option value="name">氏名</option>
		        		<option value="email">メールアドレス</option>
		        	</select>
		        	
		        	<label for="search_type" class="inline">検索キー</label>
		        	<?php echo $this->formText("search_key")?>
		        </p>
		        
		        <p>
        			<label for="status" class="inline">会議室タイプ</label>
		        	<?php echo $this->formSelect("status", NULL, NULL, $this->status_list)?>
		        	
		        	<button type="submit" id="save_btn">検索</button>
		        </p>
	        </form>
    	</div>
	</div>

	<!-- 一覧 -->
	<div id="table_container">
		<?php echo $this->partial("/component/table.phtml", array('table'=>$table,'table_translate'=>$this->table_translate))?>
	</div>
	
	<?php echo $this->formHidden("msg", $this->msg);?>
	
	
<!-- User confirm dialog -->
<div id="user-confirm-dialog" style="display:none">
	<form class="form" id="user-confirm-form">
			<fieldset class="grey-bg inline-label">
				<legend>ユーザ情報</legend>
				<?php echo $this->formHidden("user_id", 0)?>
				<p>
					<label for="useruser">ユーザ名</label>
					<span id="username"></span>
				</p>
				
				<p>
					<label for="name">氏名</label>
					<span id="name"></span>
				</p>
				
				<p>
					<label for="type">ユーザタイプ</label>
					<span id="type"></span>
				</p>
				
				<p>
					<label for="email">メールアドレス</label>
					<span id="email"></span>
				</p>
				
				<p>
					<label for="phone">電話番号</label>
					<span id="phone"></span>
				</p>
			</fieldset>
		</form>
</div>
	
	
</section>

<?php else :?>
	<!-- Ajax mode -->
	<!-- Search mode load by ajax -->
	<?php echo $this->partial("/component/table.phtml", array('table'=>$table,'table_translate'=>$this->table_translate))?>

<?php endif;?>


<?php if(!$search_mode):?>
<!-- Table script -->
<script type="text/javascript" src="/js/table.js"></script>
<script type="text/javascript">
	$(document).ready(function(){
		$("#search_form").submit(function(){
			refreshTable();
			return false;
		});

		$("#status").change(function(){
			refreshTable();
			return false;
		});
		
		if($("#msg").val() != null && $("#msg").val() != ''){
			notify($("#msg").val());
		}
	});	

	function refreshTable(){
		var ajax_url = "/admin/user/index";
		
		var datas = "search_type=" + $("#search_type").val();
		datas += "&search_key=" + $.trim($("#search_key").val());
		datas += "&line_per_page=" + $("#line_per_page").val();
		datas += "&page=" + $("#page").val();
		datas += "&search=1";

		if($("#status").val() != "" && $("#status").val() != undefined)
			datas += "&status=" + $("#status").val();
		
		$(".black-cell span").attr('class','loading');
		jQuery.ajax({
			  url: ajax_url,
			  type: "POST",
			  data: datas,
			  success: function(data) {
				  $("#table_container").html(data);
			  },
			  complete: function(){	
				   $(".black-cell span").attr('class','success');
			  },
			  error: function() {
				  alert("Have error when loading data from server");
			  }
		});
	}

	function doConfirm(link_object, user_id){
		var row = $(link_object).parent().parent().parent().parent();
		var data_cells = $(row).find("td");

		//get row data
		var username = $(data_cells[0]).text();
		var type = $(data_cells[1]).text();
		var name = $(data_cells[2]).text();
		var email = $(data_cells[3]).text();
		var phone = $(data_cells[4]).text();

		//set data to dialog
		$("#user-confirm-form #username").html(username);
		$("#user-confirm-form #type").html(type);
		$("#user-confirm-form #name").html(name);
		$("#user-confirm-form #email").html(email);
		$("#user-confirm-form #phone").html(phone);
		$("#user-confirm-form #user_id").val(user_id);
		
		openUserConfirmDialog();
	}
	
	function openUserConfirmDialog()
	{
		$.modal({
			content: $("#user-confirm-dialog").html(),
			title: 'ユーザ承認',
			maxWidth: 500,
			buttons: {
				"閉じる": function(win) { win.closeModal(); },
				"承認": function(win) {
					confirmUser($("#user_id").val()); 
					win.closeModal(); 
				}
			}
		});
	}

	function confirmUser(user_id){
		var ajax_url = "/admin/user/confirm";
		
		var datas = "user_id=" + user_id;
		
		jQuery.ajax({
			  url: ajax_url,
			  type: "POST",
			  data: datas,
			  success: function(data) {
				  refreshTable();
			  },
			  complete: function(){	
			  },
			  error: function() {
				  alert("サーバエラー123");
			  }
		});

		return false;
	}
</script>
<?php endif;?>